package schedule8087.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import schedule8087.entity.param.ScheduleAddParam;
import schedule8087.entity.po.Schedule;
import schedule8087.entity.vo.ResultVO;
import schedule8087.service.ScheduleService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ttxxi
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Resource
    ScheduleService scheduleService;

    @ApiOperation("新增时刻表排班")
    @PostMapping
    public ResultVO addSchedule(@RequestBody ScheduleAddParam param){
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(param, schedule, Schedule.class);
        boolean res = scheduleService.save(schedule);
        if (res){
            return new ResultVO.SUCCESS();
        }else {
            return new ResultVO.FAIL();
        }
    }

    @GetMapping
    @ApiOperation("根据destinationId查询时刻表，不传则查全部")
    public ResultVO<List<Schedule>> getByDestination(Long destinationId){
        LambdaQueryWrapper<Schedule> queryWrapper = new LambdaQueryWrapper<Schedule>()
                .eq(!ObjectUtils.isEmpty(destinationId), Schedule::getDestinationId, destinationId);
        return new ResultVO.SUCCESS<>(scheduleService.list(queryWrapper));
    }

    @DeleteMapping
    @ApiOperation("根据id删除")
    public ResultVO deleteById(Long id){
        boolean res = scheduleService.removeById(id);
        if (res){
            return new ResultVO.SUCCESS();
        }else {
            return new ResultVO.FAIL();
        }
    }

}

